package com.sangeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.constants.SystemConstants;
import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.entity.Article;
import com.sangeng.domain.entity.Category;
import com.sangeng.domain.vo.CategoryListVo;
import com.sangeng.domain.vo.CategoryVo;
import com.sangeng.domain.vo.PageVo;
import com.sangeng.mapper.CategoryMapper;
import com.sangeng.service.ArticleService;
import com.sangeng.service.CategoryService;
import com.sangeng.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2023-09-30 10:23:51
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private ArticleService articleService;
    /**
     * 分类查询列表
     * @return
     */
    @Override
    public ResponseResult getCategoryList() {
        //1.查询文章表，状态为正式发布
        //1.1封装条件，状态为正式发布
        LambdaQueryWrapper<Article> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> articleList = articleService.list(lqw);
        //2.获取文章列表中每个文章的分类id,去重
        Set<Long> categoryIds= articleList.stream()
                .map(article -> article.getCategoryId())
                .collect(Collectors.toSet());
        //3.查询分类表，通过分类id查询
        List<Category> categories = listByIds(categoryIds);
        //4.对查询出来的分类列表进行过滤，选择正常状态的分类
        List<Category> categoryList= categories.stream()
                .filter(category -> (SystemConstants.CATEGORY_STATUS_NORMAL).equals(category.getStatus()))
                .collect(Collectors.toList());
        //对结果进行封装
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categoryList, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }
    /**
     * 获取所有的分类
     * @return
     */
    @Override
    public ResponseResult<CategoryListVo> listAllCategory() {
        List<Category> list = list();
        List<CategoryListVo> categoryListVos = BeanCopyUtils.copyBeanList(list, CategoryListVo.class);
        return ResponseResult.okResult(categoryListVos);
    }
    /**
     * 分类分页查询
     * 需要分页查询分类列表。能根据分类名称进行模糊查询。
     能根据状态进行查询。
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @Override
    public ResponseResult<PageVo> getAllCategory(Integer pageNum, Integer pageSize,
                                                 String name, String status) {
        LambdaQueryWrapper<Category> lqw=new LambdaQueryWrapper<>();
        lqw.eq(status!=null,Category::getStatus,status);
        lqw.like(name!=null,Category::getName,name);
        Page<Category> page=new Page<>(pageNum,pageSize);
        page(page,lqw);
        return ResponseResult.okResult(new PageVo(page.getRecords(),page.getTotal()));
    }
    /**
     * 新增分类
     * @param category
     * @return
     */
    @Override
    public ResponseResult saveData(Category category) {
        save(category);
        return ResponseResult.okResult();
    }
    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @Override
    public ResponseResult<Category> getDataById(Long id) {
        Category category = getById(id);
        return ResponseResult.okResult(category);
    }
    /**
     * 更新数据
     * @param category
     * @return
     */
    @Override
    public ResponseResult updateData(Category category) {
        updateById(category);
        return ResponseResult.okResult();
    }
    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteDataById(Long id) {
        removeById(id);
        return ResponseResult.okResult();
    }
}
